package com.example.lin.homework_yjj_login.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lin.homework_yjj_login.R;
import com.example.lin.homework_yjj_login.model.ResultObj;
import com.example.lin.homework_yjj_login.utils.HttpUtils;
import com.google.gson.Gson;

import java.util.HashMap;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_forgetPsw;
    private TextView tv_registerNow;
    private EditText et_userName;
    private EditText et_psw;
    private View tv_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }



    private void initView() {
        et_userName = findViewById(R.id.et_userName);
        et_psw = findViewById(R.id.et_psw);
        tv_forgetPsw = findViewById(R.id.tv_forgetPsw);
        tv_registerNow = findViewById(R.id.tv_registerNow);

        tv_login = findViewById(R.id.tv_login);

        tv_login.setOnClickListener(this);
        tv_forgetPsw.setOnClickListener(this);
        tv_registerNow.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login:
                loginView();
                break;
            case R.id.tv_forgetPsw:
//                Toast.makeText(this, "点击了忘记密码", Toast.LENGTH_SHORT).show();
//                startActivity(new Intent(this, ForgetActivity.class));
                break;
            case R.id.tv_registerNow:
//                Toast.makeText(this, "点击了立即注册", Toast.LENGTH_SHORT).show();
                goToRegisterActivity();
                break;

        }
    }

    private AlertDialog alertDialog;

    private void showAlertDialog(String message) {
        alertDialog = new AlertDialog.Builder(this).setTitle("警告")
                .setMessage(message).setNegativeButton("确认", null).show();

    }

    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            Object obj = msg.obj;
            switch (msg.what) {
                case 1:
                    ResultObj resultObj = new Gson().fromJson(obj.toString(), ResultObj.class);
                    if ("9999".equals(resultObj.getCode())) {
                        Toast.makeText(LoginActivity.this, resultObj.getMsg(), Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                        finish();
                    } else {
                        showAlertDialog(resultObj.getMsg());
                    }
                    break;
                case 2:
                    Toast.makeText(LoginActivity.this, "error:" + obj, Toast.LENGTH_SHORT).show();
                    break;
            }
            return false;
        }
    });


    private void loginView() {
        String username = et_userName.getText().toString();
        String psw = et_psw.getText().toString();
        String address = "http://192.168.3.159:8080/home/login.do";
        HashMap<String, String> map = new HashMap<>();
        map.put("userName", username);
        map.put("psw", psw);
        HttpUtils.post(address, map, new HttpUtils.OnHttpCallback() {
            @Override
            public void onSuccess(String response) {
                h.sendMessage(h.obtainMessage(1, response));
            }

            @Override
            public void onFailure(String error) {
                h.sendMessage(h.obtainMessage(2, error));
            }
        });


    }

    private void goToRegisterActivity() {
        startActivity(new Intent(this, RegisterActivity.class));
    }


    private long lastTime = 0;


    /**
     * 当 Android 的返回按键 按下的时候.该方法被调用
     */
    @Override
    public void onBackPressed() {
//        super.onBackPressed();//Activity默认的行为 (默认会结束掉自己),可以覆盖掉父类默认的行为
        long l = System.currentTimeMillis();
        if (l - lastTime < 3000) {
            finish();//结束Activity
        } else {
            Toast.makeText(this, "再按一次退出!", Toast.LENGTH_LONG).show();
        }
        lastTime = l;
    }


}
